import {compose, lifecycle, withHandlers, withState} from "recompose"
import {kernel} from "../../shared/libs/browser"
import { Icon as LegacyIcon } from '@ant-design/compatible';
import { Modal } from "antd";
import React from "react"
import {HOST} from "../../shared/project-config"
import { ExclamationCircleOutlined } from '@ant-design/icons'

//登录类型
const LoginType = {
  ACCOUNT: 1,
  QQ: 2,
  WX: 3,
}

const loginCompose = compose(
  withState('_submitting', '_setSubmitting', false),
  //密码登录接口信息
  withState('_serverMessage', '_setServerMessage', null),
  //验证码登录接口信息
  withState('_serverMessageSms', '_setServerMessageSms', null),
  withState('_loginType', '_setLoginType', LoginType.ACCOUNT),
  withState('activeKey', '$activeKey', "1"),
  withState('avatar', '$avatar', null),
  withState('wxLoading', '$wxLoading', true),
  withState('qqLoading', '$qqLoading', true),
  withHandlers({
    changeLoginType: ({_setLoginType, $wxLoading, $qqLoading}) => {
      return ($loginType) => {
        return () => {
					if ($loginType == LoginType.WX) {
						// $wxLoading(true)
            // _setLoginType($loginType)
            window.open(`${HOST}/open/wx?redirect=admin`,'_blank')
            return
					}
					if ($loginType == LoginType.QQ) {
						//$qqLoading(true)
            window.open(`${HOST}/open/qq?redirect=admin`,'_blank')
					}

        }
      }
    },
    toRegister: ({history}) => {
      return () => {
        history.push("/register")
      }
    },
  }),
  withHandlers({
    onActiveKeyChange: ({$activeKey, $avatar}) => {
      return (ak) => {
        $activeKey(ak)
        if (ak === '2') {
          const passwordAccount = localStorage.getItem('dynamicPhoneNo')
          const localAvatar = localStorage.getItem(`${passwordAccount}_avatar`)
          console.info(localAvatar)
          if (localAvatar && localAvatar !== 'null') {
            $avatar(localAvatar)
          }
          return
        }

        if (ak === '1') {
          const passwordAccount = localStorage.getItem('passwordAccount')
          const localAvatar = localStorage.getItem(`${passwordAccount}_avatar`)
          if (localAvatar && localAvatar !== 'null') {
            $avatar(localAvatar)
          }
        }

      }
    },
    onForgetPassword: ({$activeKey}) => {
      return () => {
        $activeKey("2")
      }
    }
  }),
  lifecycle({
    componentDidMount() {
      // preloadMenu()
      const browserKernel = kernel()
      if (!browserKernel || browserKernel.toLowerCase() !== 'chrome') {
        Modal.confirm({
          title: '浏览器检测',
          icon: <ExclamationCircleOutlined />,
          content: <div>检测到您使用的浏览器不是<b>Google浏览器</b>,请下载<b>Google浏览器</b>以获得最佳体验</div>,
          onOk: () => {
            window.location.href = `${HOST}/common/chrome/win/64`
          },
          okText: <div><LegacyIcon type={'cloud-download'}/> 立即下载</div>,
          cancelText: '谢谢,暂不下载'
        })
      }
      window.authLock = false
    }
  })
)

export default loginCompose
export {LoginType}